package client;

import java.net.*;
import java.io.*;
import java.util.*;

public class client {

	private String host ="localhost";
	private int port = 8000;
	private Socket socket;
	
	public client() throws IOException
	{
		socket = new Socket(host,port);
	}
	
	private PrintWriter getwriter(Socket socket) throws IOException
	{
		OutputStream sockerout =  socket.getOutputStream();
		return new PrintWriter(sockerout,true);
	}
	private BufferedReader getReader(Socket socket)throws IOException
	{
		InputStream socketin = socket.getInputStream();
		return new BufferedReader(new InputStreamReader(socketin));
	}
	
	public void talk() throws IOException
	{
		try{
			BufferedReader reader = getReader(socket);
			PrintWriter writer = getwriter(socket);
			BufferedReader localreader = new BufferedReader(new InputStreamReader(System.in));
			String msg = null;
			while((msg=localreader.readLine())!=null)
			{
				writer.println(msg);
				System.out.println(reader.readLine());
				if(msg.equals("exit"))
				{
					break;
				}
			}
		}catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
		finally{
			try{
			if(socket!=null)
			{
				socket.close();
			}
			}catch(IOException e)
			{
				System.out.println(e.getMessage());
			}
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		try{
		new client().talk();
		}catch(Exception exception)
		{
			System.out.println(exception.getMessage());
		}
	}

}
